﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.OpenGL;
using System.Drawing;
using Silvermoon.Images;
using Silvermoon.Windows;
using Silvermoon.UI;
using ShoppingList.DAL;
using System.ComponentModel;
using ShoppingList.DTO;

namespace ShoppingList
{

    public class CheckItem : TitleDetailsImageItem
    {
        private static readonly Color TransparentColor = PhoneColors.FromArgb(0, 255, 255, 255);


        public CheckItem()
            : base()
        {
            ImageBackground = SysImages.Circle;
            Image = SysImages.Check;
            ImageColor = TransparentColor;
            TitleLineAlignment = Alignment.Center;
            StateManager = ItemStateManagers.ItemManagerWhiteSelection;
        }

        protected override void Dispose(bool dispose)
        {
            ShoppingItem = null;
            base.Dispose(dispose);
        }

        public ShoppingItem ShoppingItem { get; private set; }

        protected bool Reverse;

        public bool IsCompleted
        {
            get
            {
                var si = ShoppingItem;
                return si != null ? si.IsCompleted : false;
            }
            set
            {
                var si = ShoppingItem;
                if (si != null)
                {
                    si.IsCompleted = value;
                    OnCompletedChanged();
                }
            }
        }

        private void OnCompletedChanged()
        {
            bool set = IsCompleted;
            if (Reverse) set ^= true;
            ColorTransition ct = new ColorTransition(300, null, set ? PhoneColors.White : TransparentColor);
            if (Host != null)
            {
                ImageColorProperty.Animate(this, ct);
            }
            else
            {
                ImageColor = ct.To.Value;
            }
        }

        public string Text
        {
            get { return Title; }
            set { Title = value; }
        }

        protected override void OnDataChanged()
        {
            var si = DataValue as ShoppingItem;
            if (ShoppingItem != si)
            {
                if (ShoppingItem != null)
                {
                    ShoppingItem.PropertyChanged -= OnPropertyChanged;
                }
                ShoppingItem = si;
                if (si != null) si.PropertyChanged += OnPropertyChanged;
            }

            if (si != null)
            {
                Text = si.Name;
                IsCompleted = si.IsCompleted;
            }
            base.OnDataChanged();
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnDataChanged();
        }

        protected override void OnTap(TapEventArgs e)
        {
            IsCompleted ^= true;
            base.OnTap(e);
        }

    }

    public class ReverseCheckItem : CheckItem
    {
        public ReverseCheckItem()
            : base()
        {
            Reverse = true;
        }
    }
}
